package com.sweets.look.ui.activity;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;

import com.sweets.look.R;
import com.sweets.look.constant.Constants;
import com.sweets.look.util.ViewUtils;

public class FileBrowser extends ListActivity {
    class SortIgnoreCase implements Comparator<File> {

        @Override
        public int compare(File f, File f1) {
            return f.getName().toLowerCase(Locale.getDefault()).compareTo(f1.getName().toLowerCase(Locale
                    .getDefault()));
        }
    }

    class ViewHolder {
        public ImageView icon;
        public TextView path;
    }

    private final BaseAdapter ba;
    private File file;
    private File[] fileList;
    private boolean hideFile;

    public FileBrowser() {
        super();
        this.hideFile = false;
        this.ba = new BaseAdapter() {
            public int getCount() {
                return FileBrowser.this.fileList.length;
            }

            public Object getItem(int position) {
                return FileBrowser.this.fileList[position];
            }

            public long getItemId(int position) {
                return ((long) position);
            }

            public View getView(int position, View convertView, ViewGroup group) {
                ViewHolder v1;
                if (convertView == null) {
                    convertView = FileBrowser.this.getLayoutInflater().inflate(R.layout.file_item,
                            group, false);
                    v1 = new ViewHolder();
                    v1.path = (TextView) convertView.findViewById(R.id.file_name);
                    v1.icon = (ImageView) convertView.findViewById(R.id.file_image);
                    convertView.setTag(v1);
                } else {
                    v1 = (ViewHolder) convertView.getTag();
                }

                File v0 = FileBrowser.this.fileList[position];
                ImageView v3 = v1.icon;
                int v2 = v0.isFile() ? R.drawable.file_txt : R.drawable.file_dic;
                v3.setImageResource(v2);
                v1.path.setText(v0.getName());
                return convertView;
            }
        };
    }

    private void finishWithResult(String path) {
        Bundle v0 = new Bundle();
        v0.putString("results", "Thanks Thanks");
        Intent v1 = new Intent();
        v1.putExtras(v0);
        v1.setDataAndType(Uri.fromFile(new File(path)), "vnd.android.cursor.dir/lysesoft.andexplorer.file");
        this.setResult(-1, v1);
        this.finish();
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.file_browser);
        Intent v1 = this.getIntent();
        this.hideFile = v1.getExtras().getBoolean("HideFile");
        v1.getData().getPath();
        if (this.hideFile) {
            this.findViewById(R.id.choseDir).setVisibility(View.VISIBLE);
        }

        this.file = new File(Constants.EXTERNAL_STORAGE + "/");
//      this.path = (TextView) this.findViewById(R.id.path);
        this.updateFileList(true, this.hideFile);


    }

    public void onToolbarBtnClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_back: {
                finish();
                break;
            }
        }
    }

    public void onFileBrowserClick(View v) {
        switch (v.getId()) {
            case R.id.dir_back: {
                if (this.file.getPath().equals("/storage")) {
                    return;
                }

                this.file = this.file.getParentFile();
                this.updateFileList(false, this.hideFile);
//                this.path.setText(this.file.getPath());
                this.ba.notifyDataSetChanged();
                break;
            }
            case R.id.choseDir: {
                this.finishWithResult(this.file.getPath());
                break;
            }
        }
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        if (this.fileList[position].isDirectory()) {
            this.file = this.fileList[position];
            this.updateFileList(false, this.hideFile);
        } else {
            this.finishWithResult(this.file.getPath() + "/" + this.fileList[position].getName());
        }
    }


    private void updateFileList(boolean isFirst, boolean hideFile) {

//      this.path.setText(this.file.getAbsolutePath());
        this.fileList = null;
        File[] v1 = this.file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                boolean v0 = !file.isHidden();
                return v0;
            }
        });
        ArrayList<File> v2 = new ArrayList<>();
        ArrayList<File> v3 = new ArrayList<>();
        ArrayList<File> v4 = new ArrayList<>();
        if (v1 != null) {
            int v6 = v1.length;
            int v5;
            for (v5 = 0; v5 < v6; ++v5) {
                File v0 = v1[v5];
                if (v0.isDirectory()) {
                    v2.add(v0);//目录
                }

                if ((v0.isFile()) && !hideFile) {
                    v3.add(v0);//文件
                }
            }
        }

        Collections.sort(v2, new SortIgnoreCase());
        Collections.sort(v3, new SortIgnoreCase());
        v4.addAll(v2);
        v4.addAll(v3);
        this.fileList = v4.toArray(new File[v4.size()]);
        if (isFirst) {
            this.setListAdapter(this.ba);
        } else {
            this.ba.notifyDataSetChanged();
        }
    }
}

